﻿using PureMVC.Interfaces;
using PureMVC.Patterns.Mediator;
using Rzc.DevPlat.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rzc.DevPlat
{
    public class FormMainMediator : Mediator
    {
        FormMain formMain;
        public FormMainMediator(object viewComponent) : base("FormMainMediator", viewComponent)
        {
            formMain = viewComponent as FormMain;
            formMain.Load += FormMain_Load;
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            SendNotification("StartupCommand", formMain);
            SendNotification("CreateHeaderNodesCommand", formMain.GetHeader);

            var headerMediator = new HeaderMenuMediator(formMain.GetHeader);
            ApplicationFacade.Instance.RegisterMediator(headerMediator);
        }
        public override string[] ListNotificationInterests()
        {
            var messages = new List<String>()
            {
            "SetStyleManager",
            "ShowErrorMessage",
            "ShowInfoMessage",
            "ShowWarnMessage",
            "OpenWorkSpaceCommand",
            "CloseWorkSpaceCommand",
            "SaveWorkSpaceCommand",
            "SaveAsWorkSpaceCommand"
            };
            return messages.ToArray();
        }
        public override void HandleNotification(INotification notification)
        {
            switch (notification.Name)
            {
                case "SetStyleManager":
                    this.formMain.GetStyleManager.Style = (Sunny.UI.UIStyle)notification.Body;
                    break;
                case "ShowErrorMessage":
                    ShowMessage((string)notification.Body,"Error");
                    break;
                case "ShowInfoMessage":
                    ShowMessage((string)notification.Body, "Info");
                    break;
                case "ShowWarnMessage":
                    ShowMessage((string)notification.Body, "Warn");
                    break;
                case "OpenWorkSpaceCommand":
                    SendNotification("OpenWorkSpaceCommand", this.formMain.GetUITabControl);
                    break;
                case "CloseWorkSpaceCommand":
                    ShowMessage((string)notification.Body, "Warn");
                    break;
                case "SaveWorkSpaceCommand":
                    ShowMessage((string)notification.Body, "Warn");
                    break;
                case "SaveAsWorkSpaceCommand":
                    ShowMessage((string)notification.Body, "Warn");
                    break;

                default:break;
            }
            base.HandleNotification(notification);
        }
        void ShowMessage(string Message,string type)
        {
            this.formMain.Invoke(new Action(()=> {
                if (type == "Error")
                {
                    this.formMain.uiLabelMessage.ForeColor = System.Drawing.Color.Red;
                }
                else if (type == "Info")
                {
                    this.formMain.uiLabelMessage.ForeColor = System.Drawing.Color.Black;
                }
                else if (type == "Warn")
                {
                    this.formMain.uiLabelMessage.ForeColor = System.Drawing.Color.Gold;
                }
                this.formMain.uiLabelMessage.Text = Message;
            }));
        }
    }
}
